package com.hteos.demo.web;

import com.hteos.biz.user.entity.User;
import com.hteos.biz.model.UserVo;
import com.hteos.biz.utils.UserContext;
import com.hteos.framework.core.protocol.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * @author LIQIU
 * @date 2018-6-26
 **/
@Controller
public class IndexController {

    @Value("${hteos.basePath:}")
    private String basePath = "";

    public static String[] THEMES = new String[]{"blue", "green", "purple", "red", "orange", "gray", "cyan"};

    public static String[] WALLPAPERS = new String[]{
            "images/wallpaper/10.jpg",
            "images/wallpaper/11.jpg",
            "images/wallpaper/cloud.jpg",
            "images/wallpaper/football.jpg",
            "images/wallpaper/fifa.jpg",
            "images/wallpaper/wallpaper.jpg"};

    @RequestMapping("/")
    public String home(Model model) {
        String theme = null;
        UserVo userVo = UserContext.getUser();
        if (userVo != null) {
            theme = userVo.getTheme();
        } else {
            theme = THEMES[new Random().nextInt(THEMES.length)];
        }
        model.addAttribute("theme", theme);
        return "/index";
    }

    @ResponseBody
    @RequestMapping("/current")
    public Result current(@SessionAttribute(name = "ACCOUNT", required = false) User user) {
        if (user == null) {
            String wallpaper = WALLPAPERS[(new Random().nextInt(WALLPAPERS.length))];
            user = new User();
            user.setWallpaper(wallpaper);
            user.setMode("metro");
        }
        user.setPassword(null);
        return Result.buildSuccess(user);
    }

    @RequestMapping("/theme/hteos.preloader.css")
    public String preloaderCss(@SessionAttribute(name = "ACCOUNT", required = false) User user,
                               HttpSession httpSession) {
        String theme = THEMES[new Random().nextInt(THEMES.length)];
        if (user != null) {
            theme = user.getTheme();
        }
        httpSession.setAttribute("theme",theme);
        return "redirect:" + basePath + "css/" + theme + "/hteos.preloader.min.css";
    }

    @RequestMapping("/theme/hteos.css")
    public String mainCss(@SessionAttribute(name = "theme") String theme) {
        return "redirect:" + basePath + "css/" + theme + "/hteos.min.css";
    }

    @RequestMapping("/index.html")
    public String index(Model model) {
        return this.home(model);
    }

}
